package cn.eonshine.cuba.inf.modular.sys.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import cn.eonshine.base.core.base.controller.BaseController;
import cn.eonshine.base.core.common.annotion.BussinessLog;
import cn.eonshine.base.core.common.annotion.Permission;
import cn.eonshine.base.core.common.constant.Const;
import cn.eonshine.base.core.common.constant.dictmap.DictMap;
import cn.eonshine.base.core.common.exception.BizExceptionEnum;
import cn.eonshine.base.core.exception.GunsException;
import cn.eonshine.base.core.util.ToolUtil;
import cn.eonshine.cuba.inf.modular.sys.entity.Dict;
import cn.eonshine.cuba.inf.modular.sys.service.IDictService;
import cn.eonshine.cuba.inf.modular.sys.warpper.DictWarpper;

/**
 * 字典控制器
 *
 * @author fengshuonan
 * @Date 2017年4月26日 12:55:31
 */
@Controller
@RequestMapping("/dict")
public class DictController extends BaseController {

    private String PREFIX = "/system/dict/";

    @Autowired
    private IDictService dictService;

    @RequestMapping("")
    public String index() {
        return PREFIX + "dict.html";
    }

    @RequestMapping("/dict_add")
    public String deptAdd() {
        return PREFIX + "dict_add.html";
    }

    @Permission(Const.ADMIN_NAME)
    @RequestMapping("/dict_edit/{dictId}")
    public String deptUpdate(@PathVariable Integer dictId, Model model) {
        Dict dict = dictService.getById(dictId);
        model.addAttribute("dict", dict);
        List<Dict> subDicts = dictService.list(new QueryWrapper<Dict>().eq("pid", dictId));
        model.addAttribute("subDicts", subDicts);
        return PREFIX + "dict_edit.html";
    }

    @BussinessLog(value = "添加字典记录", key = "dictName,dictValues", dict = DictMap.class)
    @RequestMapping(value = "/add")
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object add(String dictName, String dictValues) {
        if (ToolUtil.isOneEmpty(dictName, dictValues)) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        this.dictService.addDict(dictName, dictValues);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/list")
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> list = this.dictService.list(condition);
        List<Map<String, Object>> lowCaseList = Lists.newArrayList();
        for (Map<String, Object> map : list) {
        	Map<String, Object> lowCaseMap = Maps.newHashMap();
			for (Map.Entry<String, Object> entry : map.entrySet()) {
				String key = entry.getKey();
				lowCaseMap.put(key.toLowerCase(), entry.getValue());
			}
			lowCaseList.add(lowCaseMap);
		}
        return super.warpObject(new DictWarpper(lowCaseList));
    }

    @RequestMapping(value = "/detail/{dictId}")
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object detail(@PathVariable("dictId") Integer dictId) {
        return dictService.getById(dictId);
    }

    @BussinessLog(value = "修改字典", key = "dictName,dictValues", dict = DictMap.class)
    @RequestMapping(value = "/update")
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object update(Integer dictId, String dictName, String dictValues) {
        if (ToolUtil.isOneEmpty(dictId, dictName, dictValues)) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        dictService.editDict(dictId, dictName, dictValues);
        return SUCCESS_TIP;
    }

    @BussinessLog(value = "删除字典记录", key = "dictId", dict = DictMap.class)
    @RequestMapping(value = "/delete")
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object delete(@RequestParam Integer dictId) {

        //缓存被删除的名称

        this.dictService.delteDict(dictId);
        return SUCCESS_TIP;
    }

}
